package web.admin.shop.product;

import domain.shop.Product;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteSettings;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.string.Strings;
import service.facade.admin.ShopAdminService;
import web.admin.AdminPage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ProductSearchPanel extends Panel {

    @SpringBean
    private ShopAdminService shopAdminService;
    private static Logger logger = Logger.getLogger(ProductSearchPanel.class.getName());
    private AutoCompleteTextField<String> field;
    private Form<Void> form;

    public ProductSearchPanel(String id, final AdminPage parent, final ProductAdminPanel productAdminPanel) {
        super(id);

        form = new Form<Void>("form");
        form.setOutputMarkupId(true);
        add(form);

        AutoCompleteSettings autoCompleteSettings = new AutoCompleteSettings();
        autoCompleteSettings.setShowListOnFocusGain(true);
        field = new AutoCompleteTextField<String>("ac",
                new Model<String>(""), autoCompleteSettings) {
            @Override
            protected Iterator<String> getChoices(String input) {
                if (Strings.isEmpty(input)) {
                    List<String> emptyList = Collections.emptyList();
                    return emptyList.iterator();
                }

                List<String> choices = new ArrayList<String>();
                Long id = null;
                try {
                    input = input.split(" ", 2)[0];
                    id = Long.valueOf(input);
                    for (Product product : shopAdminService.findProductsByIdLike(id)) {
                        String choice = product.getId() + " - " + product.getName();

                        choices.add(choice);
                        if (choices.size() == 16) {
                            break;
                        }
                    }
                } catch (NumberFormatException e) {
                    choices.add("Zadejte ID");
                }
                return choices.iterator();
            }
        };
        form.add(field);
        Button button = new AjaxButton("submit") {
            @Override
            protected void onSubmit(AjaxRequestTarget ajaxRequestTarget, Form<?> form) {

                String objectAsString = field.getDefaultModelObjectAsString();
                try {
                    Long id1 = null;
                    objectAsString = objectAsString.split(" ", 2)[0];

                    id1 = Long.valueOf(objectAsString);
                    try {
                        if (shopAdminService.findProductById(id1) != null) {

                            productAdminPanel.replacePanel(id1, ajaxRequestTarget);
                            field.setModelObject("");
                            ajaxRequestTarget.addComponent(field);
                        }
                    } catch (Exception e) {
                        logger.warning(e.getMessage());
                    }
                } catch (NumberFormatException e) {
                    field.clearInput();
                    ajaxRequestTarget.addComponent(field);

                } catch (Exception e) {
                    logger.log(Level.SEVERE, e.getMessage(), e);
                }
            }
        };
        form.add(button);

    }


}
